package org.jsola.hr.helper;

import org.jsola.common.DateKit;
import org.jsola.common.SafeKit;
import org.jsola.hr.dto.PositionInfoAddDTO;
import org.jsola.hr.dto.PositionInfoUpdateDTO;
import org.jsola.hr.entity.PositionInfoDO;
import org.jsola.hr.query.PositionInfoQuery;
import org.jsola.hr.service.IPositionInfoService;
import org.jsola.hr.vo.PositionInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

import static org.jsola.hr.core.AbstractTest.SITE_ID;



/**
 * @author zhr
 */
@Component
public class PositionInfoTestHelper extends BaseTestHelper {
    
    @Autowired
    private IPositionInfoService positionInfoService;

    /**
     * 生成add测试数据
     * @return PositionInfoAddDTO
     */
    public PositionInfoAddDTO getPositionInfoAddDTO(){
        PositionInfoAddDTO positionInfoAddDTO = new PositionInfoAddDTO();
        positionInfoAddDTO.setType(SafeKit.getInteger(getRandomValue("tinyint",4)));
        positionInfoAddDTO.setName(getRandomValue("varchar",255));
        positionInfoAddDTO.setCode(getRandomValue("varchar",32));
        positionInfoAddDTO.setRemark(getRandomValue("varchar",2000));
        positionInfoAddDTO.setDesc(getRandomValue("varchar",2000));
        positionInfoAddDTO.setFormalNum(SafeKit.getInteger(getRandomValue("int",11)));
        positionInfoAddDTO.setCompanyId(getRandomValue("varchar",32));
        positionInfoAddDTO.setLevel(SafeKit.getInteger(getRandomValue("int",10)));
        positionInfoAddDTO.setSort(SafeKit.getInteger(getRandomValue("int",10)));
        positionInfoAddDTO.setParentId(getRandomValue("varchar",32));
        return positionInfoAddDTO;
    }


    /**
     * 生成update测试数据
     * @return PositionInfoUpdateDTO
     */
    public PositionInfoUpdateDTO getPositionInfoUpdateDTO(PositionInfoVO positionInfoVO){
        return positionInfoService.selectDOById(positionInfoVO.getId(), SITE_ID).to(PositionInfoUpdateDTO.class);
    }

    /**
     * 生成查询数据
     * @return PositionInfoQuery
     */
    public PositionInfoQuery getPositionInfoQuery() {
        PositionInfoQuery positionInfoQuery = new PositionInfoQuery();
        List<PositionInfoDO> positionInfoDOList = positionInfoService.list(new PositionInfoDO(),SITE_ID);
        if (!CollectionUtils.isEmpty(positionInfoDOList)) {
            PositionInfoDO positionInfoDO = positionInfoDOList.get(0);
            positionInfoQuery = positionInfoDO.to(PositionInfoQuery.class);
            if (positionInfoDO.getGmtCreate() != null) {
                positionInfoQuery.setGmtCreateStart(DateKit.getBeforeDay(positionInfoDO.getGmtCreate(),-1));
                positionInfoQuery.setGmtCreateEnd(DateKit.getBeforeDay(positionInfoDO.getGmtCreate(),1));
            }
            if (positionInfoDO.getGmtModified() != null) {
                positionInfoQuery.setGmtModifiedStart(DateKit.getBeforeDay(positionInfoDO.getGmtModified(),-1));
                positionInfoQuery.setGmtModifiedEnd(DateKit.getBeforeDay(positionInfoDO.getGmtModified(),1));
            }
        }
        return positionInfoQuery;
    }

    /**
     * 保存示例
     * @return PositionInfoVO
     */
    public PositionInfoVO savePositionInfoExample(){
        PositionInfoAddDTO addDTO = this.getPositionInfoAddDTO();
        PositionInfoVO positionInfoVO = positionInfoService.save(addDTO,buildTokenUser());
        return positionInfoVO;
    }


}

